/**
 * @desc 用户相关路由
 */
import express, { Request, Response } from 'express';
import { CGetUserInfo, CLogin, CUserOption, CUserAddTicker } from '../controller/user';
import { CRelationLogin } from '../controller/userRelation';
import ErrorCode from '../utils/errorCode';
const {
  ErrorUserVotParamErr,
} = ErrorCode;

// 获取路由
const router = express.Router();

// 查询用户信息
router.post('/user/list', async (req: Request, res: Response) => {
  // 获取首页用户数据
  const result: any = await CGetUserInfo(req.body) as any;
  res.json(result);
});

// 保存用户信息
router.post('/user/save', async (req: Request, res: Response) => {
  const result: any = await CUserOption(req.body) as any;
  res.json(result);
});

// 用户投票接口
router.post('/user/vote', async (req: Request, res: Response) => {
  const { openid, passopenid } = req.body;
  if (!openid || !passopenid) {
    res.json(ErrorUserVotParamErr);
    return;
  }
  const result: any = await CUserAddTicker(openid, passopenid) as any;
  res.json(result);
});

// 登录接口
router.post('/user/login', async function (req: Request, res: Response) {
  const result: any = await CLogin(req.body) as any;
  res.json(result);
})

// 投票人登录接口
router.post('/user/relationLogin', async function (req: Request, res: Response) {
  const result: any = await CRelationLogin(req.body) as any;
  res.json(result);
})
export default router;